import Header from '../../components/Header/Header.vue';
import { mapSelectOpions } from '../../lib/utils';
import { Ajax } from '../../lib/ajax';
export default {
    name: 'ProjectList',
    components: {
        'ivu-header': Header,
    },
    data() {
        return {
            isLoading: false,
            headerOption: {
                searchBar: {
                    isShow: false,
                },
                isScrolling: false,
            },
            queryData: {
                bizId: '',
                projId: '',
            },
            bizList: null,
            projList: null,
        };
    },
    computed: {
        bizSelectOptions() {
            return mapSelectOpions(this.bizList, 'bizId', 'bizName');
        },
        projSelectOptions() {
            return mapSelectOpions(this.projList, 'projId', 'projName');
        },
    },
    created() {
        this.handleScroll();
        this.ajaxGetBizList();
        this.ajaxGetProjectList();
    },
    methods: {
        handleScroll() {
            window.addEventListener('scroll', () => {
                this.headerOption.isScrolling = document.documentElement.scrollTop > 20;
            });
        },
        ajaxGetBizList() {
            this.isLoading = true;
            Ajax.get({
                url: '/api/biz/list',
                success: (res) => {
                    if (res.code.toString() === '200') {
                        this.bizList = res.data;
                        this.isLoading = false;
                    } else {
                        this.isLoading = false;
                    }
                },
                error: () => {
                    this.isLoading = false;
                }
            });
        },

        /**
         * ajaxGetProjectList 获取项目列表
         * @param  {string} bizId 业务线id
         */
        ajaxGetProjectList(bizId) {
            this.isLoading = true;
            Ajax.get({
                url: '/api/project/list',
                params: {
                    bizId: bizId || '',
                },
                success: (res) => {
                    if (res.code.toString() === '200') {
                        this.projList = res.data;
                        this.isLoading = false;
                    } else {
                        this.isLoading = false;
                    }
                },
                error: () => {
                    this.isLoading = false;
                }
            });
        },

        handleQuery() {
            if (!this.queryData.bizId) {
                this.$Message.error({
                    content: '请先选择业务线'
                });
                return;
            }

            // 校验选择业务线不为空
            this.ajaxGetProjectList();
        },

        /**
         * gotoApiList 跳转至接口页
         * @param  {string} projId 项目id（必选）
         */
        gotoApiList(projId) {
            this.$router.push({
                name: 'apiList',
                params: {
                    projId,
                },
            });
        },

        /**
         * gotoEditProject 跳转至项目编辑页
         * @param  {string} projId 项目id（可选）
         */
        gotoEditProject(projId) {
            const routerConf = {
                name: 'projectEdit',
            };

            if (projId) {
                routerConf.params = {
                    projId: projId,
                };
            }

            this.$router.push(routerConf);
        },
    },
    watch: {
        isLoading(newVal) {
            if (newVal) {
                this.$Spin.show();
            } else {
                this.$Spin.hide();
            }
        },
    },
};
